var mgePost = {
    tableid: "table1",
    layerindex: null,
    bsTable: null,
    idselect: null,
    layoutopt: null
};
mgePost.openDlg = function (title) {
    var hei = $(window).height() - 20;
    var wi = $(window).width() - 20;
    layer.open({
        type: 1,
        title: title ? title : "添加",
        area: [(wi > 650 ? 650 : wi) + 'px', (hei > 500 ? 500 : hei) + 'px'],
        shade: 0.4,
        maxmin: true,
        btn: ["确定", "取消"],
        content: $("#postopt"),
        scrollbar: false,
        success: function (layero, index) {

        },
        yes: function (index) {
            $("#form_opt").submit();
            mgePost.layerindex = index;
        }, btn2: function (index) {
            mgePost.clearInputs();
        }, cancel: function () {
            mgePost.clearInputs();
        }
    });
};

mgePost.clearInputs = function () {
    var ipts = $("#form_opt").find("[data-type='rep']");
    if (ipts.length > 0) {
        ipts.each(function (i, v) {
            $(v).val("");
            var er = $(v).next();
            if (er != null && $(er).length > 0 && $(er).prop("nodeName").toLowerCase() == "em") {
                er.remove();
                var hs = $(v).parents(".col-md-8");
                hs.removeClass("has-success");
                hs.removeClass("has-error");
            }
        });
    }
}

mgePost.initColumn = function () {
    return [[{
        field: 'selectItem', radio: true
    }, {
        title: '岗位id', field: 'id', width: "100px", align: 'center', valign: 'middle'
    }, {
        title: '岗位名称', field: 'postName', width: "200px", align: 'center', valign: 'middle', formatter: function (v) {
            return v;
        }
    }, {
        title: '岗位编码', field: 'postCode', width: "200px", align: 'center', valign: 'middle', formatter: function (v) {
            return v;
        }
    }, {
        title: '岗位描述', field: 'remark', width: "150px", align: 'center', valign: 'middle', formatter: function (v) {
            return v;
        }
    }, {
        title: '创建时间', field: 'createTime', width: "150px", align: 'center', valign: 'middle', formatter: function (v) {
            return v;
        }
    }, {
        title: '操作', align: 'center', valign: 'middle', width: '150px',
        formatter: function (value, row, index) {
            var actions = [];
            actions.push('<a class="btn btn-success btn-xs " href="javascript:void(0)" onclick="mgePost.updateShow(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
            actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick=" mgePost.ajaxDelete(\'' + row.id + '\');"><i class="fa fa-trash"></i>删除</a>');
            return actions.join('');
        }
    }
    ]];
}
mgePost.queryParams = function () {
    return cmm.serializeObject($("#form_search"));
}
mgePost.resHeigth = function () {
    var he = $(window).height() - ($(".search_cos").outerHeight(true) + 50);
    return he;
}
mgePost.addOnClick = function () {
    $("#add").on("click", function (e) {
        e.preventDefault();
        mgePost.clearInputs();
        mgePost.openDlg();
    });
}

mgePost.updateOnClick = function () {
    $("#update").on('click', function (e) {
        e.preventDefault();
        var row = mgePost.bsTable.getSelections();
        if (row == null && row.length == 0) {
            layer.alert("请选中您要修改数据！");
        }
        // mgePost.openDlg("修改");
        // mgePost.getByRoleid(row[0].id);
        mgePost.updateShow(row[0].id);
    });
}
mgePost.updateShow = function (id) {
    mgePost.openDlg("修改");
    mgePost.getByPostid(id);
}
mgePost.getByPostid = function (id) {
    var lay = layer.load({shade: [0.1]});
    $.ajax({
        url: ctx + "/mge/adx/post/getByid",
        data: {id: id},
        dataType: "json",
        type: 'post',
        success: function (d, s) {
            layer.close(lay);
            if (d.code == '200') {
                cmm.fromData(d.data);
                $("#form_opt").valid();
            } else {
                layer.alert(d.message);
            }
        }, error: function (sta) {
            layer.close(lay);
            layer.alert("请求失败，请重试或检查网络！");
        }
    });
}
mgePost.ajaxDelete = function (id) {
    var conf = layer.confirm("您确定要删除数据吗？", {title: ["确认", "font-size:14px;font-weight:bold;"]}, function () {
        layer.close(conf);
        var lay = layer.load({shade: [0.1]});
        $.ajax({
            url: ctx + "/mge/adx/post/delete",
            data: {ids: row[0].id},
            type: "post",
            dataType: "json",
            success: function (data, sta) {
                layer.close(lay);
                if (data != null && data.code == '200') {
                    cmm.layerMessge("删除成功");
                    mgePost.refresh();
                }
            },
            error: function (sta) {
                layer.close(lay);
                layer.alert("请求失败，请重试或检查网络！");
            }
        })
    }, function () {

    });
}
mgePost.delOnClick = function () {
    $("#del").on('click', function (e) {
        e.preventDefault();
        var row = mgePost.bsTable.getSelections();
        if (row == null || row.length == 0) {
            layer.alert("请选中您要修改数据！");
            return false;
        }
        mgePost.ajaxDelete(row[0].id)
    });

}
mgePost.postAjaxFrom = function () {
    $("#form_opt").validate({
        rules: {
            postName: {
                "required": true, maxlength: 20, textChvail: true
            },
            postSort: {number: true, max: 9999999, min: 1},
            remark: {"required": true, maxlength: 100, textChvail: true},
        }, messages: {
            postName: {"required": "请输入岗位名称", maxlength: "最大20字符"},
            postSort: {},
            remark: {"required": "请输入描述", maxlength: "最大100字符"},
        }, success: function (label) {

        }, onkeyup: function (element) {
            $(element).valid();
        }, onfocusout: false, errorElement: "em", errorPlacement: function (error, element) {
            $(error).css({"color": "red", "font-size": "15px"});
            $(element).parent().append(error);
        }, highlight: function (element, errorClass, validClass) {
            $(element).parents(".col-md-8").addClass("has-error").removeClass("has-success");
        }, unhighlight: function (element, errorClass, validClass) {
            $(element).parents(".col-md-8").addClass("has-success").removeClass("has-error");
        }, submitHandler: function () {
            if (!$("#form_opt").valid()) {
                return false;
            }
            mgePost.ajax();
            return false;
        }
    });
}
mgePost.refresh = function (pageNumber) {
    mgePost.bsTable.refresh(mgePost.queryParams(), pageNumber);
}
mgePost.load = function () {
    mgePost.bsTable.load(mgePost.queryParams());
}
mgePost.ajax = function () {
    var lay = layer.load(0, {shade: [0.1]});
    // if ($("#id").val() == "") {
    //     $.ajax({
    //         url: ctx + "/mge/adx/addRole",
    //         data: $("#form_opt").formSerialize(),
    //         type: "post",
    //         dataType: "json",
    //         success: function (data, stat, jqh) {
    //             layer.close(lay);
    //             layer.close(mgePost.layerindex);
    //             mgePost.clearInputs();
    //             if (data.code == '200') {
    //                 mgePost.refresh(1);
    //                 cmm.layerMessge("添加成功！");
    //             } else {
    //                 layer.alert(data.message);
    //             }
    //         },
    //         error: function (d) {
    //             layer.close(lay);
    //             layer.alert("请求失败请重试或检查网络！");
    //         }
    //     });
    // } else {
    $.ajax({
        url: ctx + "/mge/adx/post/addOrUpdate",
        data: $("#form_opt").formSerialize(),
        type: "post",
        dataType: "json",
        success: function (data, stat, jqh) {
            layer.close(lay);
            layer.close(mgePost.layerindex);
            mgePost.clearInputs();
            if (data.code == '200') {
                mgePost.refresh();
                if ($("#id").val() == "") {
                    cmm.layerMessge("添加成功！");
                } else {
                    cmm.layerMessge("修改成功！");
                }
            } else {
                layer.alert(data.message);
            }
        },
        error: function (d) {
            layer.close(lay);
            layer.alert("请求失败请重试或检查网络！");
        }
    });
    // }
}
var myLayout;
$(function () {
    // myLayout = $('body').layout({
    //     north__minSize: 50
    //     , north__maxSize: 400,
    //     togglerContent_open: '<div class="ui-icon"></div>',
    //     togglerContent_closed: '<div class="ui-icon"></div>'
    // });
    $("#form_search").on("submit", function (e) {
        mgePost.bsTable.destroy();
        getData();
        return false;
    });
    mgePost.addOnClick();
    mgePost.updateOnClick();
    mgePost.delOnClick();
    mgePost.postAjaxFrom();
    getData();
});

function getData() {
    var bs = new BgdataTable();
    var opt = new BgOption(mgePost.tableid, ctx + "/mge/adx/post/list?rad=" + new Date().getTime(), mgePost.initColumn(), mgePost.queryParams(), mgePost.resHeigth());
    opt.toolbarId = "table1Toolbar";
    bs.draw(opt);
    mgePost.bsTable = bs;
}